package com.qmerak.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @author shaohong.cheng
 * @date 2013-6-4
 */
public class ExcelUtil {
	public static List<Map<String, Object>> read(InputStream inputStream, int startRow) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		try {
			Workbook workbook = new HSSFWorkbook(inputStream);
			Sheet sheet = workbook.getSheetAt(0);
			int rowNum = 0;
			for (Row row : sheet) {
				if (rowNum++ < startRow - 1) {
					continue;
				}
				HashMap<String, Object> rowData = new HashMap<String, Object>();
				int cellNum = 0;
				for (Cell cell : row) {
					switch (cell.getCellType()) {
					case Cell.CELL_TYPE_STRING:
						rowData.put(String.valueOf(cellNum), cell.getStringCellValue());
						break;
					case Cell.CELL_TYPE_NUMERIC:
						rowData.put(String.valueOf(cellNum), cell.getNumericCellValue());
						break;
					default:
						break;
					}
					cellNum++;
				}
				result.add(rowData);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File file = new File("E:\\TDDOWNLOAD\\人员信息导入格式0603.xls");
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			List<Map<String, Object>> datas = read(fileInputStream, 2);
			System.out.println(datas);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}
